using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public static class MessageCenter
{
    private static Dictionary<MessageEnum, System.Collections.Generic.List<Action>> messages = new Dictionary<MessageEnum, System.Collections.Generic.List<Action>>(10);

    public static void Send(MessageEnum messageName)
    {
        System.Collections.Generic.List<Action> callbacks;
        if (messages.TryGetValue(messageName, out callbacks))
        {
            if (callbacks != null)
                foreach (var cb in callbacks)
                {
                    cb();
                }
        }
    }

    public static void Register(MessageEnum messageName, System.Action callback)
    {
        System.Collections.Generic.List<Action> callbacks;

        if (!messages.TryGetValue(messageName, out callbacks))
        {
            callbacks = new System.Collections.Generic.List<Action>();
            messages.Add(messageName, callbacks);
        }
        callbacks.Add(callback);
    }

    public static void UnRegister(MessageEnum messageName, System.Action callback)
    {
        System.Collections.Generic.List<Action> callbacks;
        if (messages.TryGetValue(messageName, out callbacks))
        {
            if (callbacks != null)
            {
                for (int i = 0; i < callbacks.Count; ++i)
                {
                    if (callbacks[i] == callback)
                        callbacks.RemoveAt(i);
                }
            }
        }
    }

    internal static void Register(object updateScroll1, object updateScroll2)
    {
        throw new NotImplementedException();
    }
}

public static class MessageCenter<T>
{
    private static Dictionary<MessageEnum, System.Collections.Generic.List<Action<T>>> messages = new Dictionary<MessageEnum, System.Collections.Generic.List<Action<T>>>(10);

    public static void Send(MessageEnum messageName, T arg)
    {
        System.Collections.Generic.List<Action<T>> callbacks;
        if (messages.TryGetValue(messageName, out callbacks))
        {
            if (callbacks != null)
                foreach (var cb in callbacks)
                {
                    cb(arg);
                }
        }
    }

    public static void Register(MessageEnum messageName, System.Action<T> callback)
    {
        System.Collections.Generic.List<Action<T>> callbacks;

        if (!messages.TryGetValue(messageName, out callbacks))
        {
            callbacks = new System.Collections.Generic.List<Action<T>>();
            messages.Add(messageName, callbacks);
        }
        callbacks.Add(callback);
    }

    public static void UnRegister(MessageEnum messageName, System.Action<T> callback)
    {
        System.Collections.Generic.List<Action<T>> callbacks;
        if (messages.TryGetValue(messageName, out callbacks))
        {
            if (callbacks != null)
            {
                for (int i = 0; i < callbacks.Count; ++i)
                {
                    if (callbacks[i] == callback)
                        callbacks.RemoveAt(i);
                }
            }
        }
    }
}

public static class MessageCenter<T1, T2>
{
    private static Dictionary<MessageEnum, System.Collections.Generic.List<Action<T1, T2>>> messages = new Dictionary<MessageEnum, System.Collections.Generic.List<Action<T1, T2>>>(10);

    public static void Send(MessageEnum messageName, T1 arg1, T2 arg2)
    {
        System.Collections.Generic.List<Action<T1, T2>> callbacks;
        if (messages.TryGetValue(messageName, out callbacks))
        {
            if (callbacks != null)
                foreach (var cb in callbacks)
                {
                    cb(arg1, arg2);
                }
        }
    }

    public static void Register(MessageEnum messageName, System.Action<T1, T2> callback)
    {
        System.Collections.Generic.List<Action<T1, T2>> callbacks;
        if (!messages.TryGetValue(messageName, out callbacks))
        {
            callbacks = new System.Collections.Generic.List<Action<T1, T2>>();
            messages.Add(messageName, callbacks);
        }
        callbacks.Add(callback);
    }

    public static void UnRegister(MessageEnum messageName, System.Action<T1, T2> callback)
    {
        System.Collections.Generic.List<Action<T1, T2>> callbacks;
        if (messages.TryGetValue(messageName, out callbacks))
        {
            if (callbacks != null)
            {
                for (int i = 0; i < callbacks.Count; ++i)
                {
                    if (callbacks[i] == callback)
                        callbacks.RemoveAt(i);
                }
            }
        }
    }
}

public static class MessageCenter<T1, T2, T3>
{
    private static Dictionary<MessageEnum, System.Collections.Generic.List<Action<T1, T2, T3>>> messages = new Dictionary<MessageEnum, System.Collections.Generic.List<Action<T1, T2, T3>>>(10);

    public static void Send(MessageEnum messageName, T1 arg1, T2 arg2, T3 arg3)
    {
        System.Collections.Generic.List<Action<T1, T2, T3>> callbacks;
        if (messages.TryGetValue(messageName, out callbacks))
        {
            if (callbacks != null)
                foreach (var cb in callbacks)
                {
                    cb(arg1, arg2, arg3);
                }
        }
    }

    public static void Register(MessageEnum messageName, System.Action<T1, T2, T3> callback)
    {
        System.Collections.Generic.List<Action<T1, T2, T3>> callbacks;
        if (!messages.TryGetValue(messageName, out callbacks))
        {
            callbacks = new System.Collections.Generic.List<Action<T1, T2, T3>>();
            messages.Add(messageName, callbacks);
        }
        callbacks.Add(callback);
    }

    public static void UnRegister(MessageEnum messageName, System.Action<T1, T2, T3> callback)
    {
        System.Collections.Generic.List<Action<T1, T2, T3>> callbacks;
        if (messages.TryGetValue(messageName, out callbacks))
        {
            if (callbacks != null)
            {
                for (int i = 0; i < callbacks.Count; ++i)
                {
                    if (callbacks[i] == callback)
                        callbacks.RemoveAt(i);
                }
            }
        }
    }
}

public static class MessageCenter<T1, T2, T3, T4>
{
    private static Dictionary<MessageEnum, System.Collections.Generic.List<Action<T1, T2, T3, T4>>> messages = new Dictionary<MessageEnum, System.Collections.Generic.List<Action<T1, T2, T3, T4>>>(10);

    public static void Send(MessageEnum messageName, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
    {
        System.Collections.Generic.List<Action<T1, T2, T3, T4>> callbacks;
        if (messages.TryGetValue(messageName, out callbacks))
        {
            if (callbacks != null)
                foreach (var cb in callbacks)
                {
                    cb(arg1, arg2, arg3, arg4);
                }
        }
    }

    public static void Register(MessageEnum messageName, System.Action<T1, T2, T3, T4> callback)
    {
        System.Collections.Generic.List<Action<T1, T2, T3, T4>> callbacks;
        if (!messages.TryGetValue(messageName, out callbacks))
        {
            callbacks = new System.Collections.Generic.List<Action<T1, T2, T3, T4>>();
            messages.Add(messageName, callbacks);
        }
        callbacks.Add(callback);
    }

    public static void UnRegister(MessageEnum messageName, System.Action<T1, T2, T3, T4> callback)
    {
        System.Collections.Generic.List<Action<T1, T2, T3, T4>> callbacks;
        if (messages.TryGetValue(messageName, out callbacks))
        {
            if (callbacks != null)
            {
                for (int i = 0; i < callbacks.Count; ++i)
                {
                    if (callbacks[i] == callback)
                        callbacks.RemoveAt(i);
                }
            }
        }
    }
}

